package org.jeecg.modules.flowable.handle;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.flowable.entity.FlowInstance;
import org.jeecg.modules.flowable.enums.FlowInstanceStatusEnum;
import org.jeecg.modules.flowable.service.FlowInstanceService;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 实例完成处理
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class InstanceCompletedHandle {

    final FlowInstanceService flowInstanceService;


    /**
     * 实例完成发送通知
     */
    public void completed(String instId) {
        log.info("instance：{} completed ！ ", instId);

        // 获取流程状态
        FlowInstance instance = this.flowInstanceService.getById(instId);
        if (Objects.isNull(instance)) {
            log.warn("instance does not exist，instance：{} ", instId);
            return;
        }
        // 流程完成需要获取最后操作用户进行赋值
        // 更新流程状态
        if (FlowInstanceStatusEnum.ONGOING.getCode().equals(instance.getInstanceStatus())) {
            instance.setInstanceStatus(FlowInstanceStatusEnum.FINISH.getCode());
        } else {
            instance.setInstanceStatus(FlowInstanceStatusEnum.TERMINATION.getCode());
        }
        this.flowInstanceService.updateById(instance);
    }
}
